package com.netintech.core.checkWeakPwd.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 密码字符验证类
 */
public class PassWordUtil {
    /**
     * 密码连续性验证 连续字符串返回true
     * 密码连续性校验是指123456,654321,abcdefg,gfedcba等数字和字母顺序是连续的字符称之为连续性弱密码
     *
     * @param password 传入的校验字符串
     * @return
     */
    public static boolean check_numl(String password) {
        boolean is = true;
        String v = isCf(password);
        if ("4".equals(v)) {
            return false;
        } else {
            return simpleLetterAndNumCheck(password, password.length());
        }
    }

    /**
     * 密码重复性验证 重复字符串返回true
     *密码重复性校验是指11111,2222,aaaa,bbbbb等数字和字母都是相同字符称之为重复性弱密码
     * @param password 传入的校验字符串
     * @return
     */
    public static boolean check_numz(String password) {
        char[] value = password.toCharArray();
        List<String> list = new ArrayList<>();
        for (char c : value) {
            list.add(String.valueOf(c));
        }
        Map<String, Long> map = new HashMap<>();
//        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(p -> p, Collectors.counting()));
//        map.forEach((k, v) -> System.out.println(k + ":" + v));
        for (String s : list) {
            map.put(s, 1L);
        }
        return map.size() == 1;
    }

    /**
     * 校验字符串连续多少位是纯数字或者纯字母(字母区分大小写)
     *
     * @param value  密码
     * @param length 校验长度,默认6为
     * @return
     */
    public static boolean simpleLetterAndNumCheck(String value, int length) {
        //是否不合法
        boolean isValidate = false;
        //
        int i = 0;
        //计数器
        int counter = 1;
        //
        for (; i < value.length() - 1; ) {
            //当前ascii值
            int currentAscii = Integer.valueOf(value.charAt(i));
            //下一个ascii值
            int nextAscii = Integer.valueOf(value.charAt(i + 1));
            //满足区间进行判断
            if ((PassWordUtil.rangeInDefined(currentAscii, 48, 57) || PassWordUtil.rangeInDefined(currentAscii, 65, 90) || PassWordUtil.rangeInDefined(currentAscii, 97, 122))
                    && (PassWordUtil.rangeInDefined(nextAscii, 48, 57) || PassWordUtil.rangeInDefined(nextAscii, 65, 90) || PassWordUtil.rangeInDefined(nextAscii, 97, 122))) {
                //计算两数之间差一位则为连续
                if (Math.abs((nextAscii - currentAscii)) == 1) {
                    //计数器++
                    counter++;
                } else {
                    //否则计数器重新计数
                    counter = 1;
                }
            }
            //满足连续数字或者字母
            if (counter >= length) return !isValidate;
            //
            i++;
        }

        //
        return isValidate;
    }

    /**
     * 判断一个数字是否在某个区间
     *
     * @param current 当前比对值
     * @param min     最小范围值
     * @param max     最大范围值
     * @return
     */
    public static boolean rangeInDefined(int current, int min, int max) {
        //
        return Math.max(min, current) == Math.min(current, max);
    }


    /**
     * 判断字符重复性
     *
     * @param str
     * @return 1 大写重复字母 2小写重复字母 3数字重复 4非重复数据
     */
    public static String isCf(String str) {
        Pattern pattern1 = Pattern.compile("[a-z]*");
        Pattern pattern2 = Pattern.compile("[A-Z]*");
        Pattern pattern3 = Pattern.compile("[0-9]*");
        Matcher matcher1 = pattern1.matcher(str);
        Matcher matcher2 = pattern2.matcher(str);
        Matcher matcher3 = pattern3.matcher(str);
        if (matcher1.matches()) {
            return "1";
        } else if (matcher2.matches()) {
            return "2";
        } else if (matcher3.matches()) {
            return "3";
        } else {
            return "4";
        }
    }
}
